package com.liusy.code.wizards;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import com.liusy.code.Activator;
import com.liusy.code.ProjectBean;
import com.liusy.code.Tools;
import com.liusy.code.bean.JavaTemplateArgs;
import com.liusy.code.bean.JspTemplateArgs;
import com.liusy.code.bean.PackageNameInfo;
import com.liusy.code.templates.ActionTemplate;
import com.liusy.code.templates.DaoImplTemplate;
import com.liusy.code.templates.DaoTemplate;
import com.liusy.code.templates.JspAddTemplate;
import com.liusy.code.templates.JspFormTemplate;
import com.liusy.code.templates.JspListTemplate;
import com.liusy.code.templates.ModelTemplate;
import com.liusy.code.templates.ServiceImplTemplate;
import com.liusy.code.templates.ServiceTemplate;


/**
 * 生成代码
 * 
 * @author liusy
 *
 */
public class CodeGenerationWizard extends DynamicPageWizard implements INewWizard
{

	private CodeGenerationWizardPage pageCode;
	private DbGenerationWizardPage pageDb;
	private TableGenerationWizardPage pageTable;
	private ISelection selection;
	private ProjectBean projectVo;
	//java代码生成
	private List<JavaTemplateArgs> javaTemplateArgsList;
	//jsp代码生成
	private List<JspTemplateArgs> jspTemplateArgsList;

	public CodeGenerationWizard()
	{
		super();
		setNeedsProgressMonitor(true); 
		setDefaultPageImageDescriptor(Activator.getImageDescriptor("icons/wizard.jpg"));
		setWindowTitle("代码生成");
	}

	@Override
	public void addPages()
	{
		pageDb = new DbGenerationWizardPage(selection);
		pageTable = new TableGenerationWizardPage(selection);
		pageCode = new CodeGenerationWizardPage(selection);
		addPage(pageDb);
		addPage(pageTable);
		addPage(pageCode);
	}
	
	
	@Override
	public boolean performFinish()
	{
		IRunnableWithProgress op = new IRunnableWithProgress()
		{
			public void run(IProgressMonitor monitor) throws InvocationTargetException
			{
				monitor.beginTask("代码生成", 100);
				//生成java代码
				String javaCode = "";
				//写文件
				String jspCode = "";
				
				//1--------------------------------定义模板-----------------------------------
				DaoTemplate daoTemplate = new DaoTemplate();
				DaoImplTemplate daoImplTemplate = new DaoImplTemplate();
				ServiceTemplate serviceTemplate = new ServiceTemplate();
				ServiceImplTemplate serviceImplTemplate = new ServiceImplTemplate();
				ActionTemplate actionTemplate = new ActionTemplate();
				JspListTemplate jspListTemplate = new JspListTemplate();
				JspFormTemplate jspFormTemplate = new JspFormTemplate();
				JspAddTemplate jspAddTemplate = new JspAddTemplate();
				ModelTemplate modelTemplate = new ModelTemplate();
				
				
				//2--------------------------------生成model--------------------------------
				try {
					javaTemplateArgsList = Tools.createEntiryJavaTemplateArgsList((IStructuredSelection)selection);
					for (JavaTemplateArgs javaTemplateArgs : javaTemplateArgsList) {
						//创建包
						IPackageFragmentRoot packageFragmentRoot = projectVo.getJavaProject().findPackageFragmentRoot(new Path(projectVo.getSrc()));
						IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(javaTemplateArgs.getPackageName());
						if (!packageFragment.exists())
						{
							packageFragment = packageFragmentRoot.createPackageFragment(javaTemplateArgs.getPackageName(), true, null);
						}
						javaCode = modelTemplate.generate(javaTemplateArgs);
						
						//getClassNameInfo
						packageFragment.createCompilationUnit(javaTemplateArgs.getClassNameInfo().getEntityClassName() + ".java", javaCode, true, new NullProgressMonitor());
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				//3-------------------------------根据model生成其它类---------------------------
				try {
					javaTemplateArgsList = Tools.createJavaTemplateArgsList((IStructuredSelection)selection);
					jspTemplateArgsList = Tools.createJspTemplateArgsList((IStructuredSelection)selection);
					System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+javaTemplateArgsList.size());
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				//4-------------------------------------DAO、Servise、Action-------------------------------------
				monitor.worked(1);
				monitor.subTask("java代码生成");
				for (JavaTemplateArgs javaTemplateArgs : javaTemplateArgsList)
				{
					try
					{
						//创建包
						IPackageFragmentRoot packageFragmentRoot = projectVo.getJavaProject().findPackageFragmentRoot(new Path(projectVo.getSrc()));
						IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(javaTemplateArgs.getPackageName());
						if (!packageFragment.exists())
						{
							packageFragment = packageFragmentRoot.createPackageFragment(javaTemplateArgs.getPackageName(), true, null);
						}

						//生成java代码
						switch (javaTemplateArgs.getType())
						{
						case DAO:
							javaCode = daoTemplate.generate(javaTemplateArgs);
							break;
						case DAOIMPL:
							javaCode = daoImplTemplate.generate(javaTemplateArgs);
							break;
						case SERVICE:
							javaCode = serviceTemplate.generate(javaTemplateArgs);
							break;
						case SERVICEIMPL:
							javaCode = serviceImplTemplate.generate(javaTemplateArgs);
							break;
						case ACTION:
							javaCode = actionTemplate.generate(javaTemplateArgs);
							break;
						}
						packageFragment.createCompilationUnit(javaTemplateArgs.getClassName() + ".java", javaCode, true, new NullProgressMonitor());
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
				//5-------------------------------------jsp代码生成(INPUT、List)-------------------------------------
				monitor.worked(1);
				monitor.subTask("jsp代码生成");
				for (JspTemplateArgs jspTemplateArgs : jspTemplateArgsList)
				{
					
					PackageNameInfo pn = (PackageNameInfo) pageCode.getResult();
					//创建文件夹
					//String directoryPath = projectVo.getWebInf() + "/jsp/" + jspTemplateArgs.getModeName() + "/" + jspTemplateArgs.getClassNameInfo().getEntityObjectName();
					String directoryPath ="";
					if(jspTemplateArgs.getType().equals(JspTemplateArgs.JspTemplateType.FORM))
						directoryPath = projectVo.getRoot() + pn.getJspPath()+"/form";
					else
						directoryPath = projectVo.getRoot() + pn.getJspPath();
					
					if(!(new File(directoryPath)).exists())
					Tools.createDirectory(directoryPath);
					//创建文件
					String filePath = directoryPath + "/" + jspTemplateArgs.getJspName();
					Tools.createFile(filePath);

					switch (jspTemplateArgs.getType())
					{
					case LIST:
						jspCode = jspListTemplate.generate(jspTemplateArgs);
						break;
					case FORM:
						jspCode = jspFormTemplate.generate(jspTemplateArgs);
						break;
					case ADD:
						jspCode = jspAddTemplate.generate(jspTemplateArgs);
						break;
					default:
						break;
					}
					Tools.writeStringToFile(filePath, jspCode);
				}
				monitor.done();
			}
		};

		try
		{
			getContainer().run(true, false, op);
		} catch (InvocationTargetException e)
		{
			e.printStackTrace();
			return false;
		} catch (InterruptedException e)
		{
			e.printStackTrace();
			return false;
		}

		/**
		 * 刷新工程
		 */
		projectVo.refresh();

		return true;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection)
	{
		this.selection = selection;
		projectVo = new ProjectBean(selection);
		try
		{
			
//			javaTemplateArgsList = Tools.createJavaTemplateArgsList(selection);
//			jspTemplateArgsList = Tools.createJspTemplateArgsList(selection);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}